package Tools;

import InterMessages.SunspotsDeployStatus;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.BlockingQueue;

/**
 * Created by IntelliJ IDEA.
 * User: leontiad
 * Date: Mar 9, 2010
 * Time: 1:33:22 PM
 * To change this template use File | Settings | File Templates.
 */
public class ExecuterStandard implements Executer {
    public void executeCommand(String input, File fdir, SunspotsDeployStatus m_sunspotsDeployStatus, JTextArea displayScreen, BlockingQueue bq) {
        ArrayList<String> command = new ArrayList<String>();
        if (System.getProperty("os.name").toLowerCase().contains("windows")) {

            command.add("cmd");
            command.add("/" + System.getenv("SystemDrive").substring(0, 1));

        } else {//Linux,Solaris,Ubuntu,Mac are using the bash
            command.add("/bin/sh");
            command.add("-c");
        }
        command.add(input);
        ProcessBuilder builder = new ProcessBuilder(command);
        builder.directory(fdir);
        Process p = null;
        try {
            p = builder.start();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        try {

            // any error message?
            StreamGobbler errorGobbler = new StreamGobblerStandard(p.getErrorStream(), "ERROR");

            // any output?
            StreamGobbler outputGobbler = new StreamGobblerStandard(p.getInputStream(), "OUTPUT");

            // kick them off
            Thread thr1 = new Thread(errorGobbler);
            Thread thr2 = new Thread(outputGobbler);
            thr1.start();
            thr2.start();

            // any error???
            int exitVal = p.waitFor();
            System.out.println("ExitValue: " + exitVal);
        } catch (Throwable t) {
            t.printStackTrace();

        }
        return ;
    }
}
